package cn.iocoder.yudao.module.promotion.redpacket.service.core;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.service.MerchantExtensionService;
import cn.iocoder.yudao.framework.business.basic.service.impl.AbstractService;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.api.promotion.redpacket.vo.RedPacketConfigVO;
import cn.iocoder.yudao.module.promotion.redpacket.dao.RedPacketConfigMapper;
import cn.iocoder.yudao.module.promotion.redpacket.pojo.po.RedPacketConfig;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 服务
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/06/21
 */
@Service
public class CoreRedPacketConfigService extends AbstractService<RedPacketConfig, RedPacketConfigVO> implements MerchantExtensionService<RedPacketConfig> {
    @Resource
    private RedPacketConfigMapper redPacketConfigMapper;

    @Override
    public RedPacketConfigMapper getInjectedMapper() {
        return redPacketConfigMapper;
    }

    @DataPermission(enable = false)
    @Override
    public RedPacketConfig getOrCreate(Long shopId) {
        CoreLambdaQueryWrapper<RedPacketConfig> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.eq(RedPacketConfig::getShopId, shopId);
        wrapper.orderByDesc(RedPacketConfig::getId);
        wrapper.limitN(1);
        RedPacketConfig entity = redPacketConfigMapper.selectOne(wrapper);
        if (entity == null) {
            entity = new RedPacketConfig();
            entity.setShopId(shopId);
            entity.setFeatureEnable(false);
            entity.setFirstAmount(0);
            entity.setContinueAmount(0);
            entity.setContinueThresholdPrice(0);
            entity.setValidationDays(7);
            entity.setNote("");
            redPacketConfigMapper.insert(entity);
        }
        return entity;
    }

}
